using System;

namespace RStein.Posterous.API
{
    internal abstract class ObjectWithId<TDerived> : IExtensionInterface, IEquatable<ObjectWithId<TDerived>>
                           where TDerived : class 
    {
        #region private variables
        private static SynchronisedSequenceGenerator m_generator = new SynchronisedSequenceGenerator(Int32.MinValue, GlobalConstants.NEW_OBJECT_ID);
        #endregion private variables
        
        #region constructors

        protected ObjectWithId(int? currentId)
        {
            if (currentId != null)
            {
                Id = (int) currentId;
                return;
            }

            generateId();
        }


        protected ObjectWithId()
        {
            generateId();
        }
        #endregion constructors
        
        #region public properties
        public int Id
        {
            get; 
            internal set;
        }
        #endregion public properties

        #region methods
        public bool Equals(ObjectWithId<TDerived> other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }
            if (ReferenceEquals(this, other))
            {
                return true;
            }

            if (!OnCustomEquals(other as TDerived))
            {
                return false;
            }
            return other.Id.Equals(Id);
        }
        

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
            {
                return false;
            }
            if (ReferenceEquals(this, obj))
            {
                return true;
            }
            if (obj.GetType() != this.GetType())
            {
                return false;
            }
            return Equals((ObjectWithId<TDerived>) obj);
        }

        public override int GetHashCode()
        {            
            int hashCode = GetCustomHashCode() == 0 ? Id.GetHashCode() : Id.GetHashCode() ^ GetCustomHashCode();
            return hashCode;
        }

        
        public virtual T GetInterface<T>() where T : class
        {
            return this as T;
        }

        public static bool operator ==(ObjectWithId<TDerived> left, ObjectWithId<TDerived> right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(ObjectWithId<TDerived> left, ObjectWithId<TDerived> right)
        {
            return !Equals(left, right);
        }

        protected virtual bool OnCustomEquals(TDerived other)
        {
            if (other == null)
            {
                return false;
            }

            return true;
        }
        protected virtual int GetCustomHashCode()
        {
            return 0;
        }

        private void generateId()
        {
            Id = m_generator.GetNextNumber();
        }

        #endregion methods
    }
}